### Load Elith Source Code

setwd('/home1/99/jc152199/brt/')
source('brt.functions.R.cjsedit.r')

### Establish out directory

out.dir = '/home1/99/jc152199/ChapterOne/'

### Load optimal models

mod.dir = '/home1/99/jc152199/brt/FINALOPTIMALMODELS/'
load(paste(mod.dir,'OptimalMaxModel.Rdata',sep=''))
load(paste(mod.dir,'OptimalMinModel.Rdata',sep=''))

### Merge contributions to make table 2

table2 = merge(max.brt.gbm.step$contributions,min.brt.gbm.step$contributions, by=c('var'))

### Change some names

names(table2)[1] = 'Variable'
names(table2)[2] = 'Min Model Contrib.'
names(table2)[3] = 'Max Model Contrib.'

### Round some decimal places

table2[,2] = round(table2[,2],2)
table2[,3] = round(table2[,3],2)

### Write out as Table 2

write.csv(table2, file=paste(out.dir,'Tables/Table2.csv',sep=''), row.names=F)

### Construct Table 1, model optimization summary

table1 = data.frame(model=c('max','min'), initial.error = c(max.brt.gbm.step$initF,min.brt.gbm.step$initF), final.error = c(max.brt.gbm.step$cv.statistics$deviance.mean,min.brt.gbm.step$cv.statistics$deviance.mean), tree.complexity = c(16,16), learning.rate = c(.05,.05), op.tree.num = c(max.brt.gbm.step$n.trees,min.brt.gbm.step$n.trees))

### Write out table 1

write.csv(table1, file=paste(out.dir,'Tables/Table1.csv',sep=''), row.names=F)